package com.ezhou.hy.common.dao;


import java.io.IOException;
import java.util.Properties;

import com.ezhou.hy.common.DaoMap;
import com.ezhou.hy.common.Resources;
import com.ezhou.hy.exception.DaoException;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>CopyRight:Copyright (c) 2008.12 </p>
 * <p>Company: </p>
 * @author Haiger
 * @version 1.0
 */
public class StandardDaoFactory  implements DaoFactory{
	
	public StandardDaoFactory(){

	}
	
	/**
	 * @throws IOException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * 
	 */
	public Object getDao(Class clazz)throws DaoException, IOException, InstantiationException, IllegalAccessException{
		return creatDaoClass(clazz);
	}
	
	/**
	 * 
	 * @param clazz
	 * @return
	 * @throws DaoException 
	 * @throws IOException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private Object creatDaoClass(Class clazz) throws DaoException, IOException, InstantiationException, IllegalAccessException{
		Properties daoMap = DaoMap.getInstance().getDaoProperties();
		Object dao = null;
		String daoifaceURI = clazz.getName();
		int length = daoifaceURI.length();
		int index = daoifaceURI.lastIndexOf(".");
//		System.out.println(index);
		String daoifaceName = daoifaceURI.substring(index+1, length);
//		System.out.println(daoifaceName);
		String daoimplName = daoMap.getProperty(daoifaceName);
//		System.out.println(daoimplName);
		Class daoClass = null;
		
		try {
			 daoClass= Resources.classForName(daoimplName);
			 dao = daoClass.newInstance();
		} catch (ClassNotFoundException e) {
			throw new DaoException();
//			e.printStackTrace();
		}
		
		return dao;
	}
	
//	private String getDaoIfaceName(String uri){
//		
//	}

}
